package com.lovegu.three;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
 * <p>
 * 使用 JavaFx 实现简易浏览器
 * </p>
 *
 * @author 老顾
 * @since 2023/4/4
 */
public class SimpleBrowser extends Application {

    private final String DEFAULT_URL = "https://www.baidu.com";
    private final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3";

    private WebView webView;
    private WebEngine webEngine;
    private TextField addressBar;
    private Button goButton;
    private Button backButton;
    private Button forwardButton;
    private Button refreshButton;
    private Slider zoomSlider;

    @Override
    public void start(Stage primaryStage) {

        // Create WebView and WebEngine
        webView = new WebView();
        webEngine = webView.getEngine();
        webEngine.setUserAgent(USER_AGENT);

        // Create Address Bar
        addressBar = new TextField(DEFAULT_URL);
        addressBar.setOnAction(event -> loadPage(addressBar.getText()));
        goButton = new Button("Go");
        goButton.setOnAction(event -> loadPage(addressBar.getText()));
        backButton = new Button("Back");
        backButton.setOnAction(event -> goBack());
        forwardButton = new Button("Forward");
        forwardButton.setOnAction(event -> goForward());
        refreshButton = new Button("Refresh");
        refreshButton.setOnAction(event -> refreshPage());

        // Create Toolbar
        HBox toolbar = new HBox(5);
        toolbar.setPadding(new Insets(5));
        toolbar.getChildren().addAll(backButton, forwardButton, refreshButton, new Separator(), addressBar, goButton);

        // Create Zoom Slider
        zoomSlider = new Slider(0.5, 2.0, 1.0);
        zoomSlider.setShowTickLabels(true);
        zoomSlider.setShowTickMarks(true);
        zoomSlider.setMajorTickUnit(0.5);
        zoomSlider.setMinorTickCount(4);
        zoomSlider.setBlockIncrement(0.1);
        zoomSlider.valueProperty().addListener((observable, oldValue, newValue) -> setZoom(newValue.doubleValue()));

        // Create BorderPane
        BorderPane root = new BorderPane();
        root.setTop(toolbar);
        root.setCenter(webView);
        root.setBottom(zoomSlider);

        // Create Scene and Stage
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("简易浏览器");
        primaryStage.show();

        // Load Default Page
        loadPage(DEFAULT_URL);
    }

    private void loadPage(String url) {
        if (!url.startsWith("http")) {
            url = "http://" + url;
        }
        webEngine.load(url);
        addressBar.setText(url);
    }

    private void goBack() {
        WebHistory history = webEngine.getHistory();
        if (history.getCurrentIndex() > 0) {
            history.go(-1);
        }
    }

    private void goForward() {
        WebHistory history = webEngine.getHistory();
        if (history.getCurrentIndex() < history.getEntries().size() - 1) {
            history.go(1);
        }
    }

    private void refreshPage() {
        webEngine.reload();
    }

    private void setZoom(double zoom) {
        webView.setZoom(zoom);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
